/**
 * Copyright (c) 2018 开源 All rights reserved.
 * <p>
 * https://www.sxyc.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.controller;


import io.renren.annotation.Login;
import io.renren.common.utils.Result;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.yckj.common.ThreadLocalUtil;
import io.renren.modules.yckj.dto.api.LoginDTO;
import io.renren.modules.yckj.service.TokenService;
import io.renren.modules.yckj.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 登录接口
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/yckj/base")
@Tag(name = "登录接口", description = "登录接口description")
@AllArgsConstructor
public class ApiLoginController {
    private final UserService userService;
    private final TokenService tokenService;

    @PostMapping("login")
    @Operation(summary = "登录")
    public Result<Map<String, Object>> login(@RequestBody LoginDTO dto) {
        //表单校验
        ValidatorUtils.validateEntity(dto);

        //用户登录
        Map<String, Object> map = userService.wxLogin(dto);

        return new Result().ok(map);
    }

    @GetMapping("phone")
    @Operation(summary = "获取手机号")
    @Parameter(name = "code", description = "code", required = true)
    public Result<String> login(@RequestParam("code") String code) throws Exception {

        //微信用户
        String map = userService.getUserPhone(code);

        return new Result().ok(map);
    }

    @Login
    @GetMapping("logout")
    @Operation(summary = "退出")
    public Result logout() {

        tokenService.expireToken(ThreadLocalUtil.getUserId());
        return new Result();
    }

}